package com.fenghongzhang.day007test;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private ViewPager vp;
    //页面的值
    private int index = 0;
    private List<Fragment> list;

    //用它里面的handler
    private ThreeFragment threeFragment;

    private  Timer timer; //翻页定时器
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what == 100){
                //无限翻页
                vp.setCurrentItem(index);
                index++;
                if(index > list.size() - 1){
                    index = 0;
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //2
        vp = (ViewPager) findViewById(R.id.vp);


        //3
        list = new ArrayList<>();
        list.add(new OneFragment());
        list.add(new TwoFragment());
        threeFragment = new ThreeFragment();
        list.add(threeFragment);

        //4
        WelcomeAdapter welcomeAdapter = new WelcomeAdapter(getSupportFragmentManager(), list);
        vp.setAdapter(welcomeAdapter);


        //1,如果第一个打开正常.
        //2,直接跳转.
        //3,boolean.
        //4,sp
        //5,
        SharedPreferences welcome = getSharedPreferences("welcome", MODE_PRIVATE);
        //1,false
        boolean isDone = welcome.getBoolean("isDone", false);
        if(isDone){//跳转
            Intent intent = new Intent(this, Main2Activity.class);
            startActivity(intent);
            finish();
        }


        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //更新布局
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        vp.setCurrentItem();
//                    }
//                });
                handler.sendEmptyMessage(100);
            }
        },0,1000);

        //翻页监听器
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(position == list.size()-1){
                    //停止自动翻页
                    timer.cancel();
                    //去通知一个fg ,开始定时器. handler
                    threeFragment.handler.sendEmptyMessage(888);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        //ture
        SharedPreferences welcome1 = getSharedPreferences("welcome", MODE_PRIVATE);
        SharedPreferences.Editor edit = welcome1.edit();
        edit.putBoolean("isDone",true);
        edit.commit();


    }
}
